import time
import random
import aip
import pymongo

db = pymongo.MongoClient('localhost', 27017)
douban = db['doubantop250']
item_info = douban['movietop250']
emo_info = douban['emotop250']
def vis_process(percent):
    repeat_times = int(percent * 10)
    bar = '感情正向->' + '😊' * repeat_times + '😭' * (10 - repeat_times) + '<-感情负向'
    # print(bar)
    return bar


client_appid = '60757637'
client_ak = 'ATbNb012Gmq1FN4pAL5Dr11U'
client_sk = '8phRQiLl0bzfpVGswFmQtK4V87o49v83'
my_nlp = aip.nlp.AipNlp(client_appid, client_ak, client_sk)

for item in item_info.find():

    # my_txt = '中国式内在的美国电影。'
    my_txt = item['quote']
    # print(my_txt)
    # print(my_nlp.sentimentClassify(my_txt))
    results = my_nlp.sentimentClassify(my_txt)
    t = random.randint(2, 5)  # 随机一个睡眠时间，减少服务器压力
    time.sleep(t)
    # print(results)
    input_txt = results['text']
    positive_prob = results['items'][0]['positive_prob']
    print(input_txt)
    bar = vis_process(positive_prob)
    print(bar)
    print('-' * 20 + '分割线' + '-' * 20)

    data = {
        'text': input_txt,
        'emo_analysis': bar
    }

    emo_info.insert_one(data)